package io.renren.modules.mybog.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.modules.mybog.entity.MessageEntity;
import io.renren.modules.mybog.service.CategoryService;
import io.renren.modules.mybog.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.mybog.dao.PostDao;
import io.renren.modules.mybog.entity.PostEntity;
import io.renren.modules.mybog.service.PostService;
import org.springframework.util.StringUtils;


@Service("postService")
public class PostServiceImpl extends ServiceImpl<PostDao, PostEntity> implements PostService {
    @Autowired
    private MessageService messageService;
    @Autowired
    private CategoryService categoryService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Object key = params.get("key");
        QueryWrapper<PostEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)) {
              wrapper.eq("title",key);
        }

        IPage<PostEntity> page = this.page(
                new Query<PostEntity>().getPage(params),
                wrapper
        );
        page.getRecords().forEach(postEntity -> {
             postEntity.setCategory(categoryService.getById(postEntity.getCategoryId()));
        });

        return new PageUtils(page);
    }


    @Override
    public Map<String, Object> findAll(Page<PostEntity> postPage, Integer id, String name) {
        QueryWrapper<PostEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("is_top");
        if(id!=0){
            queryWrapper.eq("category_id",id);
        }
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("title",name);
        }
        List<PostEntity> list = page(postPage, queryWrapper).getRecords();
        if(list.size()>0){
            for (PostEntity post : list) {
                post.setCounts(messageService.counts(post.getId()));
            }
            Map<String,Object> map=new HashMap<>();
            map.put("list",list);
            map.put("hasNext",postPage.hasNext());
            map.put("page",postPage.getCurrent());
            map.put("pages",postPage.getPages());
            return map;
        }
      return null;
    }

    /**
     * 根据id查询
     * @return
     */
    @Override
    public PostEntity listMessage(Integer id) {
        PostEntity post = getById(id);
        QueryWrapper<MessageEntity> messageQueryWrapper=new QueryWrapper<>();
        messageQueryWrapper.eq("post_id",id);
        List<MessageEntity> list = messageService.list(messageQueryWrapper);
        List<MessageEntity> collect = list.stream().filter((pmsCategoryEntity -> {
            return pmsCategoryEntity.getFatherId() ==0;
        })).map(menu->{
            menu.setMessages(getChider(menu,list));
            return menu;
        }).collect(Collectors.toList());
        post.setMessages(collect);
        post.setCategory(categoryService.getById(post.getCategoryId()));
        return post;
    }
    //递归查找所有菜单的子菜单
    private List<MessageEntity> getChider(MessageEntity root,List<MessageEntity> all){
        List<MessageEntity> collect = all.stream().filter(categoryEntity -> {
            return categoryEntity.getFatherId() ==root.getId();
        }).map(categoryEntity -> {
            //1.找到子菜单
            categoryEntity.setMessages(getChider(categoryEntity, all));
            return categoryEntity;
        }).collect(Collectors.toList());
        return collect;
    }
}